
// sid=84b07fb3-6eeb-4976-a9c9-520a88602e79&sign=2C62F0A87DAE1FDF5093CA3E6712F44B&ts=1665628862&noncestr=167
var sessionId = tools.getSearch("sessionId");
var sign = tools.getSearch("sign");
var nocstr = tools.getSearch("noncestr");
var ts = tools.getSearch("ts");
var gladFlag; // 是否提交过

// 获取页面数据
function getPageInfo() {
    $.ajax({
        type: "get",
        url: "/adapter/unit/getData",
        data: {
            sessionId
        },
        beforeSend: function () {
            // 加载中
            $(".ui-loading-block").attr("class", "ui-loading-block show");
        },
        complete: function () {
            // 加载结束
            $(".ui-loading-block").attr("class", "ui-loading-block hide");
        },
        success: function (data) {
            // 请求失败打印异常信息
            if (data.returnCode === 1) {
                $(".btn").css("display", 'none');
                layer.open({
                    content: data.returnMessage,
                    skin: 'msg',
                    time: 2 //2秒后自动关闭
                });
                return
            }
            // 已经评价过
            if (data.jsonString) {
                var result = JSON.parse(data.jsonString)
                $(".btn").css("display", 'none');
                $('.optionbox').html('')
                $('.textareabox').css("display", 'none');
                // 已解决、未解决
                $(".card .item:first-child .select i").removeClass('selected');
                $(".card .item:first-child .select[data-id='" + result.sessionSolution + "'] i").addClass('selected');
                // 星值 1个星传5,2个星传4,3个星传3,4个星传2,5个星传1
                var starNum = [5, 4, 3, 2, 1][result.starValue - 1];
                for (let i = 0; i < starNum; i++) {
                    $('.starbox .star').eq(i).addClass('active')
                }
                if (result.starValue >= 3) {
                    var selected_items = result.selectItems && result.selectItems.split(',')
                    if (selected_items.length > 0) {
                        for (let i = 0; i < selected_items.length; i++) {
                            $('.optionbox').append(`<div class="option">${selected_items[i]}</div>`)
                        }
                    }
                    $('.text').text(result.reason)
                    $('.opinion').addClass('show')
                }
            }

        },
        error: function () {
            layer.open({
                content: "网络错误",
                skin: 'msg',
                time: 2 //2秒后自动关闭
            });
        }
    });
}
getPageInfo();

$(".card .item .select").on("click", function () {
    if (gladFlag) {
        return;
    }
    $(this).siblings().find('i').removeClass('selected');
    $(this).find('i').addClass("selected");
})

$(".btn").on("click", function () {
    // 是否解决
    var solve = $(".card .item:first-child").find('.selected').parent().data('id');
    // 星值
    var star_value = [5, 4, 3, 2, 1][$('.starbox .active').length - 1];
    // 选项值
    var selected_items = [];
    $('.optionbox .active').map((i, v) => {
        selected_items.push($(v).text())
    })
    selected_items = selected_items.join(',')
    // 原因
    var reason = $('.textareabox textarea').val();
    if (!solve || !star_value) {
        layer.open({
            content: "请完成评价再提交！",
            skin: 'msg',
            time: 2 //2秒后自动关闭
        });
        return;
    }
    if ($('textarea').val().length > 50) {
        layer.open({
            content: "最多可输入50个字符",
            skin: 'msg',
            time: 2 //2秒后自动关闭
        });
        return;
    }
    $.ajax({
        type: "post",
        url:  "/adapter/unit/recordEvalResult",
        contentType: 'application/json',
        data: JSON.stringify({
            sessionId,
            sign,
            nocstr,
            solve, // solve: 是否解决  1 解决  2 未解决
            // satisfied, // 是否满意  2：满意 3：一般  4：不满意
            star_value, // 星值  1个星传5,2个星传4,3个星传3,4个星传2,5个星传1
            selected_items: star_value >= 3 ? selected_items : '', // 选项值，逗号分隔
            reason: star_value >= 3 ? reason : '', // 原因备注
            ts
        }),
        beforeSend: function () {
            // 加载中
            $(".ui-loading-block").attr("class", "ui-loading-block show");
        },
        complete: function () {
            // 加载结束
            $(".ui-loading-block").attr("class", "ui-loading-block hide");
        },
        success: function (data) {
            console.log(data);
            if (data.returnCode == 0) {
                layer.open({
                    content: "评价成功！",
                    skin: 'msg',
                    time: 2 //2秒后自动关闭
                });
                // 刷新页面信息
                getPageInfo();
            } else {
                layer.open({
                    content: data.returnMessage,
                    skin: 'msg',
                    time: 2 //2秒后自动关闭
                });
            }
        },
        error: function () {
            layer.open({
                content: "网络错误",
                skin: 'msg',
                time: 2 //2秒后自动关闭
            });
        }
    });
})

var isInput;
$('textarea')[0].addEventListener('compositionstart', function (e) {
    isInput = true;
    console.log(isInput)
}, false)
$('textarea')[0].addEventListener('compositionupdate', function (e) {
    isInput = true;
    console.log(isInput)
}, false)
$('textarea')[0].addEventListener('compositionend', function (e) {
    isInput = false;
    wordLimit();
}, false)

// 字数显示
$('textarea').on('input', function () {
    if (isInput) {
        return;
    }
    wordLimit();
})

function wordLimit() {
    document.querySelector('textarea').setAttribute('maxlength', dealCurrentMaxLen($('textarea').val(), 50))
    $('.textareabox i').text($('textarea').val().length)
    if ($('textarea').val().length > 50) {
        layer.open({
            content: "最多可输入50个字符",
            skin: 'msg',
            time: 2 //2秒后自动关闭
        });
    }
}

function dealCurrentMaxLen(str, maxLen) {
    if (isiOS) {
        let reg = new RegExp(/\n/g);
        let lineNum = str.match(reg);
        return lineNum ? lineNum.length + maxLen : maxLen
    } else {
        return maxLen
    }
}

// 点击星星
$('.starbox .star').on('click', function () {
    if (gladFlag) {
        return;
    }
    $('.starbox .star').removeClass('active')
    $(this).addClass('active')
    $(this).prevAll().addClass('active')
    if ($('.starbox .active').length <= 3) {
        $('.opinion').addClass('show')
    } else {
        $('.opinion').removeClass('show')
        // $('.optionbox .option').removeClass('active')
        // $('.textareabox textarea').val('')
    }
})

// 点击标签
$('.optionbox .option').on('click', function () {
    if (gladFlag) {
        return;
    }
    $(this).toggleClass('active')
})
